﻿using JzSport.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JzSport.Api
{
    /// <summary>
    /// 创建活动
    /// POST
    /// ./api/v1.1/action.ashx?sys:cmd=createactivity
    /// form:
    ///     op:title=???
    ///     &op:categorycode=???
    ///     &op:address=???
    ///     &op:city=???
    ///     &op:longitude=???
    ///     &op:latitude=?
    ///     &op:begintime=?
    ///     &op:endtime=?
    ///     &op:maxpersoncount=?
    ///     &op:costpreperson=?
    ///     &op:descritpion=?
    ///     &op:images=?
    /// </summary>
    class CreateActivityAction : AuthPostAction<Params.CreateActivityParams,
        Results.CreateActivityResult>
    {
        protected override ActionResult<Results.CreateActivityResult> OnExecute(ActionContext context, 
            SysParameters sysParameters,
            Params.CreateActivityParams opParams)
        {
            List<Guid> imageIdList = new List<Guid>();
            List<int> friendIdList = new List<int>();

            if (!string.IsNullOrEmpty(opParams.Images))
            {
                string[] imageArr = opParams.Images.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                for (int index = 0; index < imageArr.Length; index++)
                {
                    imageIdList.Add(new Guid(imageArr[index]));
                }
            }

            if (!string.IsNullOrEmpty(opParams.InviteFriends))
            {
                string[] friendsArr = opParams.InviteFriends.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                for (int index = 0; index < friendsArr.Length; index++)
                {
                    friendIdList.Add(Convert.ToInt32(friendsArr[index]));
                }
            }

            ServiceResult<Activity> serviceResult = new ActivityService().Create(context.AccountId,
                opParams.Title,
                opParams.CategoryCode,
                opParams.Address,
                opParams.City,
                opParams.Longitude,
                opParams.Latitude,
                opParams.BeginTime,
                opParams.EndTime,
                opParams.MaxPersonCount,
                opParams.CostPrePerson,
                opParams.Descritpion,
                imageIdList,
                friendIdList);

            Results.CreateActivityResult actionResult = new Results.CreateActivityResult();
            if (serviceResult.ErrorCode == ErrorCode.NoError)
            {
                actionResult.ActivityId = serviceResult.Data.Id;

                return new ActionResult<Results.CreateActivityResult>(true, serviceResult.ErrorCode,
                    ErrorCode.ToString(serviceResult.ErrorCode), actionResult);
            }

            return new ActionResult<Results.CreateActivityResult>(false, serviceResult.ErrorCode,
                    ErrorCode.ToString(serviceResult.ErrorCode), actionResult);
        }
    }
}
